package 刷题.leedcode203;

import java.util.List;

public class Solution203_2 {
    public ListNode removeElements(ListNode head,int val){
        //删除值相同的头节点后，可能新的头节点值也相等，用循环解决
        while(head != null && head.val == val){
            head = head.next;
        }
        if(head == null){
            return head;
        }
        //定义一个新节点代替head头节点
        ListNode prev = head;
        while(prev.next != null){
            if(prev.next.val == val){
                prev.next = prev.next.next;
            }else{
                prev = prev.next;
            }
        }
        return head;
    }
}
